package com.orangelife.mobile.property.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.common.util.MoneyUtil2;
import com.common.util.OrangePhoneUtil;
import com.curry.android.util.MoneyUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.view.EmptyLayout;
import com.orangelife.mobile.property.adapter.PropertyBillBaseInfoAdapter;
import com.orangelife.mobile.property.adapter.PropertyBillHistoryDetailAdapter;
import com.orangelife.mobile.property.biz.PropertyHttpBiz;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.widget.ScrollListView;
import com.curry.android.http.OrangeDataHandler;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 账单详情 - 历史缴费详情
 * <p/>
 * Created by wgf on 2016/5/11.
 *
 * @author wgf
 * @since v3.6.4
 */
public class PropertyBillHistoryDetailActivity extends OrangeLifeBaseActivity {
    @Bind(R.id.ib_title_left)
    LinearLayout ibTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_title_right)
    ImageView ivTitleRight;
    @Bind(R.id.tvTotal)
    TextView tvTotal;
    @Bind(R.id.tvAmountPaid)
    TextView tvAmountPaid;
    @Bind(R.id.tvTotalAmount)
    TextView tvTotalAmount;
    @Bind(R.id.tvDeductionMoney)
    TextView tvDeductionMoney;
    @Bind(R.id.lvPropertyBillBaseInfo)
    ScrollListView lvPropertyBillBaseInfo;
    @Bind(R.id.lvPropertyBillOrderInfo)
    ScrollListView lvPropertyBillOrderInfo;
    @Bind(R.id.tvFailTip)
    TextView tvFailTip;
    @Bind(R.id.tvBillStatus)
    TextView tvBillStatus;
    @Bind(R.id.tvTel)
    TextView tvTel;
    @Bind(R.id.tvBottomPhone)
    TextView tvBottomPhone;
    @Bind(R.id.llNoHistory)
    LinearLayout llNoHistory;
    @Bind(R.id.emptyLayout)
    EmptyLayout emptyLayout;
    @Bind(R.id.tvVoucherMoney)
    TextView tvVoucherMoney;

    private List<Map<String, Object>> mBaseInfoList = new ArrayList<>();
    private List<Map<String, Object>> mFeeList = new ArrayList<>();
    private PropertyBillBaseInfoAdapter mBaseInfoAdapter;
    private PropertyBillHistoryDetailAdapter mFeeListAdapter;
    /** 缴费未成功提示文字 */
    private String tip = "其他人通过其他渠道缴纳了该账单中的费用\n如您已被扣款，请联系橙子客服处理";
    /** 房屋ID */
    private String apartmentID;
    /** 小区ID */
    private String commID;
    /** 缴费账单Id */
    private String payBillID;
    /** 缴费结果，成功或者失败 */
    private String billStatusHanzi = "";

    /** 房屋可抵扣金额 */
    private String mDeductionMoney = "0";
    /** 实付金额 */
    private String mAmountPaid = "0";
    /** 应缴总金额 */
    private String mTotalAmount = "0";
    /** 1成功 2已失效 3失败 */
    private String viewCode = "1";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_property_history_detail);
        ButterKnife.bind(this);
        getIntentInfo();
        initView();
        requestFeeHistoryDetail();
        setListener();
    }

    private void getIntentInfo() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            apartmentID = bundle.getString("apartmentID");
            commID = bundle.getString("commID");
            payBillID = bundle.getString("payBillID");
        }
    }

    private void initView() {
        tvTitle.setText("账单详情");
        ivTitleRight.setVisibility(View.INVISIBLE);
    }

    private void setListener() {
        tvTel.setOnClickListener(listener);
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestFeeHistoryDetail();
            }
        });
        tvBottomPhone.setOnClickListener(listener);
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            OrangePhoneUtil.callCustomerService2(PropertyBillHistoryDetailActivity.this);
        }
    };

    /** 设置缴费状态，缴费成功1或者失效2 缴费失败3 */
    private void setBillStatus() {
        if (viewCode.equals("3")) {
            tvBillStatus.setTextColor(getResources().getColor(R.color.lightGray));
            tvFailTip.setVisibility(View.VISIBLE);
            tvFailTip.setText(tip);
            tvTel.setText(getString(R.string.service_phone_number));
            tvTel.setVisibility(View.VISIBLE);
        } else {
            if (viewCode.equals("1")) {//成功是橙色
                tvBillStatus.setTextColor(getResources().getColor(R.color.lightOrange));
            } else {
                tvBillStatus.setTextColor(getResources().getColor(R.color.lightGray));
            }
            tvFailTip.setVisibility(View.GONE);
            tvTel.setVisibility(View.GONE);
            tvBottomPhone.setVisibility(View.VISIBLE);
            tvBottomPhone.setText(getString(R.string.service_phone_number));
        }
        tvBillStatus.setText(billStatusHanzi);
    }

    /**
     * 获取我的物业缴费信息
     */
    private void requestFeeHistoryDetail() {
        PropertyHttpBiz.getFeeHistoryDetail(commID, apartmentID, payBillID, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                emptyLayout.dismiss();
                if (OrangeErrorHandler.getInstance().isSuccess(response)) {
                    Map<String, Object> map = OrangeDataHandler.getMapWithEntityNoGson(response);
                    handleFeeHistoryDetail(map);
                } else if (errCode == Integer.parseInt(VolleyHttpUtil.ERR_CODE_NET_EXCEPTION)) {
                    emptyLayout.setErrorType(EmptyLayout.SERVER_ERROR);
                } else {
                    emptyLayout.setErrorType(EmptyLayout.NODATA);
                }
            }
        });
    }

    private void handleFeeHistoryDetail(Map<String, Object> map) {
        if (map != null) {
            emptyLayout.dismiss();
            mAmountPaid = String.valueOf(map.get("totalCost"));//实缴金额
            apartmentID = String.valueOf(map.get("apartmentID"));
//            billStatus = String.valueOf(map.get("orderStatus"));
            billStatusHanzi = String.valueOf(map.get("status"));
            viewCode = String.valueOf(map.get("viewCode"));
            mTotalAmount = String.valueOf(map.get("goodsCost"));//总额
            String voucherMoney = String.valueOf(map.get("voucherMoney"));//抵用券金额
            mDeductionMoney = String.valueOf(map.get("subMoney"));//抵扣金额
            tvVoucherMoney.setText(MoneyUtil2.handleMoney(voucherMoney, true));//抵用券
            tvAmountPaid.setText(MoneyUtil.handleMoney(mAmountPaid));
            tvDeductionMoney.setText(MoneyUtil2.handleMoney(mDeductionMoney, true));
            setData();
            setBillStatus();
            mBaseInfoList = (List<Map<String, Object>>) map.get("baseInfo");
            setBaseInfoAdapter();
            mFeeList = (List<Map<String, Object>>) map.get("orderInfo");
            setOrderInfoAdapter();
        } else {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
        }
    }


    /**
     * 设置界面上的账单信息
     */
    private void setData() {
        tvTotalAmount.setText(MoneyUtil.handleMoney(mTotalAmount));
    }

    /**
     * 设置账单条目的信息
     */
    private void setBaseInfoAdapter() {
        mBaseInfoAdapter = new PropertyBillBaseInfoAdapter(PropertyBillHistoryDetailActivity.this, mBaseInfoList);
        lvPropertyBillBaseInfo.setAdapter(mBaseInfoAdapter);
    }

    /**
     * 设置账单条目的信息
     */
    private void setOrderInfoAdapter() {
        if (mFeeList != null && mFeeList.size() == 0) {
            llNoHistory.setVisibility(View.VISIBLE);
        } else {
            mFeeListAdapter = new PropertyBillHistoryDetailAdapter(PropertyBillHistoryDetailActivity.this, mFeeList);
            lvPropertyBillOrderInfo.setAdapter(mFeeListAdapter);
        }
    }

}
